package fss.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import fss.util.rmi.RmiId;

public class FssUtils {
    private static final String DATE_FORMAT = "yyyy.MM.dd-hh.mm.ss.SS";
    /**
     * Format the date to the standardized format (MM-dd-yyyy hh.mm.ss.SS).
     * @param date the date.
     * @return the formatted date string.
     */
    public static String formatDate(Date date) {
        return formatDate(date, DATE_FORMAT);
    }
    /**
     * Format the date as the specified format.
     * @param date the date.
     * @param format the format.
     * @return the formatted date string.
     */
    public static String formatDate(Date date, String format) {
        String sDate = null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        sDate = formatter.format(date);
        return sDate;
    }
    /**
     * Get the file separator.
     * @return the separator.
     */
    public static String getSystemFileSeparator() {
        return System.getProperty("file.separator");
    }
    /**
     * Create the directories represented by the specified path.
     * @param path the path.
     * @return true if success; false otherwise.
     */
    public static boolean createDirectory(String path) {
        File file = null;
        file = new File(path);
        boolean success = false;
        if(!file.exists()) {
            success = file.mkdirs();
        }
        return success;
    }
    /**
     * Get a file byte array from the specified location and filename.
     * @param homedir the directory.
     * @param filename the filename
     * @return a byte array of the file data.
     * @throws IOException error reading the file.
     */
    public static byte[] getFileBytes(String homedir, String filename) throws IOException {
        byte[] data = null;
        FileInputStream fin = null;
        String _filename = null;
        if(homedir != null) {
            _filename = homedir + getSystemFileSeparator() + filename;
        } else {
            _filename = filename;
        }
        File file = new File(_filename);
        
        try {
            fin = new FileInputStream(file);
            data = new byte[(int)file.length()];
            fin.read(data);
            return data;
        } catch (IOException e) {
            throw e;
        } finally {
            if(fin != null) fin.close();
        }
        
    }
    /**
     * Remove the file indicated by the filename (may include full path).
     * @param filename the filename.
     * @return true, if removed; otherwise, false.
     */
    public static boolean removeFile(String filename) {
        boolean removed = false;
        File file = new File(filename);
        if(file.exists()) {
           removed = file.delete(); 
        }
        return removed;
    }
    /**
     * Convert a byte array to string.
     * @param data the byte array.
     * @return the string; or "null" if the specified data is null.
     */
    public static String byteArrayToString(byte[] data) {
        if(data == null) return "null";
        StringBuilder buf = new StringBuilder();
        buf.append(new String(data));
        return buf.toString();
    }
    /**
     * A command line utility that prompts the user for information
     * to build an RMI identifier.
     * @param title a title for the inputs.
     * @return a rmi identifier.
     */
    public static RmiId getRmiId(String title) {
        Scanner in = new Scanner(System.in);
        RmiId id = null;
        String host = null;
        String name = null;
        int port;
        if(title != null) System.out.println(title);
        System.out.print("Enter host: ");
        host = in.nextLine();
        System.out.print("Enter port: ");
        port = Integer.parseInt(in.nextLine());
        System.out.print("Enter object name: ");
        name = in.nextLine();
        id = new RmiId(host, name, port);
        return id;
    }
    /**
     * A command line utility to display a menu and get user selection.
     * @param title the menu title (can be null).
     * @param options required menu options.
     * @return the selection, or -1 if the user input an invalid option.
     */
    public static int getMenuOption(String title, String[] options) {
        Scanner stdin = new Scanner(System.in);
        int choice = -1;
        System.out.println(title != null ? title : "-- Main Menu -- ");
        for(int i = 0; i < options.length; i++) {
            System.out.println("" + (i+1) + ". " + options[i]);
        }
        System.out.print("> ");
        String input = stdin.next();
        try {
            choice = Integer.parseInt(input);
        } catch (Exception e) {
            // ignore
        }
        return choice;
    }
    /**
     * Given a directory, build a list of all the file(s) in that directory.
     * @param dir the root data directory.
     * @return a list of file(s) or an empty list.
     */
    public static List<File> getFileList(String dir) {
        List<File> files = new ArrayList<File>();
        File directory = new File(dir);
        if(directory.exists()) {
            for(File f : directory.listFiles()) {
                files.add(f);
            }
        }
        return files;
    }
}
